#include "qelib.h"
#include "FreeRTOS.h"
#include "task.h"
#include "xtime_l.h"
#include <sleep.h>
#include <stdio.h>



qe_time_t qe_time(void)
{
	qe_uint tick = xTaskGetTickCount();
	return tick / configTICK_RATE_HZ;
}

qe_time_t qe_time_ms(void)
{
	qe_time_t ms;
	qe_uint tick = xTaskGetTickCount();
	ms = tick * 1000 / configTICK_RATE_HZ;
	return ms;
}

qe_time_t qe_time_us(void)
{
	qe_time_t us;
	XTime clks;
	XTime_GetTime(&clks);
	us = clks / 400;
	return us;
}

void qe_msleep(qe_time_t ms)
{
	vTaskDelay(pdMS_TO_TICKS(ms));
}

void qe_usleep(qe_time_t us)
{
	usleep(us);
}

void qe_timeval_get(qe_timeval *tv)
{
	qe_time_t ms = qe_time_ms();
	tv->sec = ms / 1000;
	tv->usec = ms % 1000 * 1000;
}
